package com.arnold.learn.mysql2kudu;

import com.arnold.learn.impala.ImpalaUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.sql.*;
import java.util.ResourceBundle;

/**
 * Created by arnold.zhu on 2017/8/11.
 */
public class MainDemo {

    private static Logger logger = LoggerFactory.getLogger(MainDemo.class);
    private static ResourceBundle rb = ResourceBundle.getBundle("couchbase");
    private static String driver = rb.getString("mysql.driver");
    private static String url = rb.getString("mysql.url");
    private static String username = rb.getString("mysql.username");
    private static String password = rb.getString("mysql.password");

    public static void main(String[] args) {
        Connection kuduConnection = ImpalaUtil.createConnection();
        try {
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, username, password);
            PreparedStatement stmt = conn.prepareStatement("select * from store_info");
            Statement kuduStmt = kuduConnection.createStatement();
            ResultSet rs = stmt.executeQuery();
            while (rs.next()) {
                String store_number = rs.getString("store_number");
                String store = rs.getString("store");
                String store_type = rs.getString("store_type");
                String territory = rs.getString("territory");
                String open_date = rs.getString("open_date");
                String updatetime = rs.getString("updatetime");
                String ownership = rs.getString("ownership");
                String address = rs.getString("address");
                String city = rs.getString("city");
                String area = rs.getString("area");

                logger.info(store_number + "_" + store + "_" + store_type + "_" + territory + "_" + open_date + "_" +
                        updatetime + "_" + ownership + "_" + address + "_" + city + "_" + area);

                if (address.contains("省")) {
                    city = address.substring(address.indexOf("省") + 1, address.indexOf("市"));
                } else {
                    city = address.substring(0, address.indexOf("市"));
                }

                String sql = "insert into kudu_stores values('" + store_number + "','" + store + "','" + store_type + "','" + territory + "','" + open_date
                        + "','" + updatetime + "','" + ownership + "','" + address + "','" + city + "','" + area + "');";
                kuduStmt.executeUpdate(sql);
            }
            rs.close();
            stmt.close();
            conn.close();
            kuduStmt.close();
            kuduConnection.close();
        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }


    }

}
